<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Home\Index;
use App\Models\Home\Category;
use DB;

class NewsController extends Controller
{
    //
    public function news(Request $request){
    	
        //select wt_form.*,count(wt_new.id) as record from wt_form left join wt_new on wt_form.id = wt_new.category_id group by wt_form.id
         $result1 = Category::select('wt_form.*',DB::raw('count(wt_new.id) as records'))->leftJoin('wt_new','wt_form.id','=','wt_new.category_id')->groupBy('wt_form.id')->get();

        $months = Index::select(DB::raw("date_format(from_unixtime(time),'%Y年%m月') AS month"),DB::raw('count(id) AS records'))->groupBy('month')->orderBy('month','desc')->get();



         $cate = new Category();
         $result = $cate ->categoryList($result1);


        $this->main['result'] = $result;
        $this->main['months'] = $months;
              
        
        if ($request->isMethod('get') && $request->input('id') !='') {

            $data = Index::where('category_id',$request->input('id'))->get();
            
            $this->main['data'] = $data;
    
           
            return view('home.news.news',$this->main);
            
        }else{

             $data = Index::orderBy('time','desc')->where('post','1')->get();
             $this->main['data'] = $data;

            
            return view('home.news.news',$this->main);


        }
    
 	
    }

     public function news_info(Request $request){

        Index::where('id',$request->input('id'))->increment('read');
      
    	$data = Index::where('id',$request->input('id'))->get();
        
        $ids= Index::where('id', '<', $request->input('id'))->orderBy('id','desc')->limit(1)->get();
        $idz= Index::where('id', '>', $request->input('id'))->orderBy('id','asc')->limit(1)->get();
        

         $this->main['data'] = $data;
         $this->main['ids'] = $ids;
         $this->main['idz'] = $idz;


    	return view('home.news.news_info',$this->main);


    }



}
